/*
 
 */
package com.jme3.gde.gui.extra.xam.model.impl;

import com.jme3.gde.gui.extra.xam.model.ElementTypeImpl;
import com.jme3.gde.gui.extra.xam.model.AttributesType;
import com.jme3.gde.gui.extra.xam.model.ControlDefinition;
import com.jme3.gde.gui.extra.xam.model.ControlType;
import com.jme3.gde.gui.extra.xam.model.EffectValueType;
import com.jme3.gde.gui.extra.xam.model.EffectsType;
import com.jme3.gde.gui.extra.xam.model.ElementType;
import com.jme3.gde.gui.extra.xam.model.ImageType;
import com.jme3.gde.gui.extra.xam.model.InteractType;
import com.jme3.gde.gui.extra.xam.model.LayerType;
import com.jme3.gde.gui.extra.xam.model.NiftyComponent;
import com.jme3.gde.gui.extra.xam.model.NiftyComponentFactory;
import com.jme3.gde.gui.extra.xam.model.NiftyModel;
import com.jme3.gde.gui.extra.xam.model.NiftyType;
import com.jme3.gde.gui.extra.xam.model.OnHoverType;
import com.jme3.gde.gui.extra.xam.model.PanelType;
import com.jme3.gde.gui.extra.xam.model.PopupType;
import com.jme3.gde.gui.extra.xam.model.RegisterEffectType;
import com.jme3.gde.gui.extra.xam.model.RegisterMouseCursorType;
import com.jme3.gde.gui.extra.xam.model.RegisterMusicType;
import com.jme3.gde.gui.extra.xam.model.RegisterSoundType;
import com.jme3.gde.gui.extra.xam.model.ResourceBundleType;
import com.jme3.gde.gui.extra.xam.model.ScreenType;
import com.jme3.gde.gui.extra.xam.model.SingleEffectType;
import com.jme3.gde.gui.extra.xam.model.SingleEffectTypeBase;
import com.jme3.gde.gui.extra.xam.model.SingleEffectTypeHover;
import com.jme3.gde.gui.extra.xam.model.SingleEffectTypeOnHover;
import com.jme3.gde.gui.extra.xam.model.StyleType;
import com.jme3.gde.gui.extra.xam.model.TextType;
import com.jme3.gde.gui.extra.xam.model.UseControlsType;
import com.jme3.gde.gui.extra.xam.model.UseStylesType;
import javax.xml.namespace.QName;
import com.jme3.gde.gui.extra.xam.model.spi.NiftyComponentBase;
import com.jme3.gde.gui.extra.xam.model.spi.ElementFactory;
import com.jme3.gde.gui.extra.xam.model.spi.NiftyExtensibilityElementBase;
import org.w3c.dom.Element;

/**
 * Nifty component factory implementation.
 *
 * @author cuongnguyen
 */
// Generated by XAM AutoGen Tool v0.2
public class NiftyComponentFactoryImpl implements NiftyComponentFactory {
    
    private NiftyModel model;
    
    /**
     * Creates a new instance of NiftyComponentFactoryImpl
     */
    public NiftyComponentFactoryImpl(NiftyModel model) {
        this.model = model;
    }    
 
    private static QName getQName(Element element, NiftyComponentBase context) {
        String namespace = element.getNamespaceURI();
        String prefix = element.getPrefix();
        if (namespace == null && context != null) {
            namespace = context.lookupNamespaceURI(prefix);
        }
        String localName = element.getLocalName();
        assert(localName != null);
        if (namespace == null && prefix == null) {
            return new QName(localName);
        } else if (namespace != null && prefix == null) {
            return new QName(namespace, localName);
        } else {
            return new QName(namespace, localName, prefix);
        }
    }

    public NiftyComponent create(Element element, NiftyComponent context) {
        // return new SCAComponentCreateVisitor().create(element, context);
        QName qName = getQName(element, (NiftyComponentBase)context);
        ElementFactory factory = ElementFactoryRegistry.getDefault().get(qName);
        return create(factory, element, context);
    }
    
    private NiftyComponent create(ElementFactory elementFactory, Element element, NiftyComponent context) {
        if (elementFactory != null ){
            return elementFactory.create(context, element);
        } else {
            return new NiftyExtensibilityElementBase(model, element);
        }
    }
    
    public NiftyComponent create(NiftyComponent context, QName qName) {
       String prefix = qName.getPrefix();
       if (prefix == null || prefix.length() == 0) {
           prefix = qName.getLocalPart();
       } else {
           prefix = prefix + ":" + qName.getLocalPart();
       }

       ElementFactory factory = ElementFactoryRegistry.getDefault().get(qName);
       Element element = model.getDocument().createElementNS(qName.getNamespaceURI(), prefix);
       return create(factory, element, context);
    }
    
    public ElementType createElementType() {
        return new ElementTypeImpl(model);
    }

    public LayerType createLayerType() {
        return new LayerTypeImpl(model);
    }

    public PanelType createPanelType() {
        return new PanelTypeImpl(model);
    }

    public ImageType createImageType() {
        return new ImageTypeImpl(model);
    }

    public TextType createTextType() {
        return new TextTypeImpl(model);
    }

    public ControlDefinition createControlDefinition() {
        return new ControlDefinitionImpl(model);
    }

    public ControlType createControlType() {
        return new ControlTypeImpl(model);
    }

    public UseStylesType createUseStylesType() {
        return new UseStylesTypeImpl(model);
    }

    public UseControlsType createUseControlsType() {
        return new UseControlsTypeImpl(model);
    }

    public RegisterSoundType createRegisterSoundType() {
        return new RegisterSoundTypeImpl(model);
    }

    public RegisterEffectType createRegisterEffectType() {
        return new RegisterEffectTypeImpl(model);
    }

    public RegisterMusicType createRegisterMusicType() {
        return new RegisterMusicTypeImpl(model);
    }

    public RegisterMouseCursorType createRegisterMouseCursorType() {
        return new RegisterMouseCursorTypeImpl(model);
    }

    public ResourceBundleType createResourceBundleType() {
        return new ResourceBundleTypeImpl(model);
    }

    public AttributesType createAttributesType() {
        return new AttributesTypeImpl(model);
    }

    public InteractType createInteractType() {
        return new InteractTypeImpl(model);
    }

    public EffectValueType createEffectValueType() {
        return new EffectValueTypeImpl(model);
    }

    public SingleEffectTypeBase createSingleEffectTypeBase() {
        return new SingleEffectTypeBaseImpl(model);
    }

    public SingleEffectType createSingleEffectType() {
        return new SingleEffectTypeImpl(model);
    }

    public SingleEffectTypeOnHover createSingleEffectTypeOnHover() {
        return new SingleEffectTypeOnHoverImpl(model);
    }

    public OnHoverType createOnHoverType() {
        return new OnHoverTypeImpl(model);
    }

    public SingleEffectTypeHover createSingleEffectTypeHover() {
        return new SingleEffectTypeHoverImpl(model);
    }

    public EffectsType createEffectsType() {
        return new EffectsTypeImpl(model);
    }

    public StyleType createStyleType() {
        return new StyleTypeImpl(model);
    }

    public PopupType createPopupType() {
        return new PopupTypeImpl(model);
    }

    public ScreenType createScreenType() {
        return new ScreenTypeImpl(model);
    }

    public NiftyType createNiftyType() {
        return new NiftyTypeImpl(model);
    }

}
